#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <sched.h>
#include <ctype.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/if.h>


#define FILE_DEV "/proc/scsi/scsi"

static int find_device()
{
    char buf[1024];
    int fd;
    int len;
    int dev_num = 0;
    char * seek = NULL;

    fd = open(FILE_DEV, O_RDONLY); // error: Too many open files
    if (fd > 0) {
        len = read(fd, buf, sizeof(buf));
        close(fd);
        printf("close file\n");
        if (len > 0) {
            seek = buf;
            while (seek) {
                seek = strstr(seek, "Host: scsi");
                if (seek) {
                    seek += strlen("Host: scsi");
                    seek = strstr(seek, "Type");
                    if (seek) {
                        seek += strlen("Type");
                        while (* seek == ' ')
                            seek++;
                        dev_num++;
                    }
                }
            }
            printf("dev_num = %d\n", dev_num);
        }
    }
    else {
        perror(FILE_DEV);
    }

    return dev_num;
}

int main()
{
    while(1) {
        find_device();
        sleep(1);
    }
}

